package com.dao;

import com.model.Shoppingcart;
import com.utils.DbUntil;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class ShoppingcartDao {

    private Connection connection;

    //在这里初始化  这里获取链接
    public ShoppingcartDao() {
        connection = DbUntil.getConnection();
    }

    //公有方法 获取所有购物车信息
    public List<Shoppingcart> getAllShoppingcarts() {
        List<Shoppingcart> shoppingcarts = new ArrayList<Shoppingcart>();
        try {
            Statement statement = connection.createStatement();
            //这里是查询select * from shoppingcart
            ResultSet rs = statement.executeQuery("select * from shoppingcart");
            while (rs.next()) {
                Shoppingcart shoppingcart = new Shoppingcart();
                shoppingcart.setShoppingcart_id(rs.getString("Shoppingcart_id"));
                shoppingcart.setUser_id(rs.getString("user_id"));
                shoppingcart.setGoods_id(rs.getString("goods_id"));
                shoppingcart.setNumber(rs.getString("number"));
                shoppingcarts.add(shoppingcart);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return shoppingcarts;
    }

    //购物车商品数量加一
    public int addShoppingCartNum(String shoppingCartId){
        int result = 0;
        try {
            Statement statement = connection.createStatement();
            Shoppingcart shoppingcart = this.getShoppingcartByShoppingCartId(shoppingCartId);
            int num = Integer.parseInt(shoppingcart.getNumber());
            num++;
            String sql = "update shoppingcart set number= '"+num+"' where shoppingcart_id='"+shoppingCartId+"'";
            result = statement.executeUpdate(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    //购物车商品数量减一
    public int subShoppingCartNum(String shoppingCartId){
        int result = 0;
        try {
            Statement statement = connection.createStatement();
            Shoppingcart shoppingcart = this.getShoppingcartByShoppingCartId(shoppingCartId);
            int num = Integer.parseInt(shoppingcart.getNumber());
            num--;
            String sql = "update shoppingcart set number= '"+num+"' where shoppingcart_id='"+shoppingCartId+"'";
            result = statement.executeUpdate(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    //通过购物车id获取购物车信息
    public Shoppingcart getShoppingcartByShoppingCartId(String shoppingCartId){
        List<Shoppingcart> shoppingcarts = new ArrayList<Shoppingcart>();
        try {
            Statement statement = connection.createStatement();
            String sql = "select * from shoppingcart where shoppingcart_id='"+shoppingCartId+"'";
            ResultSet rs = statement.executeQuery(sql);
            while (rs.next()) {
                Shoppingcart shoppingcart = new Shoppingcart();
                shoppingcart.setShoppingcart_id(rs.getString("Shoppingcart_id"));
                shoppingcart.setUser_id(rs.getString("user_id"));
                shoppingcart.setGoods_id(rs.getString("goods_id"));
                shoppingcart.setNumber(rs.getString("number"));
                shoppingcarts.add(shoppingcart);
            }
            if(shoppingcarts.size()>0){
                return shoppingcarts.get(0);
            }
            else {return null;}
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    //通过用户id获取购物车信息
    public List<Shoppingcart> getShoppingcartByUserId(String userId){
        List<Shoppingcart> shoppingcarts = new ArrayList<Shoppingcart>();
        try {
            Statement statement = connection.createStatement();
            String sql = "select * from shoppingcart where user_id='"+userId+"'";
            ResultSet rs = statement.executeQuery(sql);
            while (rs.next()) {
                Shoppingcart shoppingcart = new Shoppingcart();
                shoppingcart.setShoppingcart_id(rs.getString("Shoppingcart_id"));
                shoppingcart.setUser_id(rs.getString("user_id"));
                shoppingcart.setGoods_id(rs.getString("goods_id"));
                shoppingcart.setNumber(rs.getString("number"));
                shoppingcarts.add(shoppingcart);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return shoppingcarts;
    }

    //通过id删除购物车信息
    public int deleteShoppingcartByShoppingcartId(String shoppingcartIds){
        int result = 0;
        try {
            Statement statement = connection.createStatement();
            String shoppingcartId[] = shoppingcartIds.split("/");
            for(String id : shoppingcartId){
                String sql = "delete from shoppingcart where shoppingcart_id='"+id+"'";
                result = statement.executeUpdate(sql);
                if(result == 0) {return result;}
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return result;
        }
        return result;
    }

    //商品加入购物车
    public int addGoodsToShoppingcart(String userId,String goodsId,String number){

        int res=0;
        try {
            Statement statement = connection.createStatement();
            String sql = "INSERT INTO shoppingcart (user_id,goods_id,number) VALUE ('"+userId+"','"+goodsId+"','"+number+"')";
            res = statement.executeUpdate(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return res;
    }
}
